//
//  XS_TBC.swift
//  EKLWisdomP3
//
//  Created by 韩云智 on 2020/10/16.
//

import RxSwift
import RxCocoa

class XS_TBC: UITabBarController {
    
    struct tabBarM {
        var title: String
        var img: String
        var select: String
        var vc: XS_BaseVC
    }
    
    override var shouldAutorotate: Bool { true }
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .portrait }
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { .portrait }

    private lazy var disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        if #available(iOS 13.0, *) { overrideUserInterfaceStyle = .light }
        tabBar.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        tabBar.tintColor = #colorLiteral(red: 0.2352941176, green: 0.8156862745, blue: 0.6509803922, alpha: 1)
        tabBar.unselectedItemTintColor = #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1)
        tabBar.isTranslucent = true
        addChildren([
            tabBarM(title: "首页", img: "icons_Tab Bar_首页备份", select: "icons_Tab Bar_首页", vc: XS_HomeVC()),
            tabBarM(title: "快学", img: "icons_Tab Bar_快学", select: "icons_Tab Bar_快学备份", vc: XS_KuaiXueVC()),
            tabBarM(title: "学习", img: "icons_Tab Bar_学习", select: "icons_Tab Bar_学习备份", vc: NestViewController()),
            tabBarM(title: "我的", img: "icons_Tab Bar_我的", select: "icons_Tab Bar_我的备份", vc: XS_MineVC.init(nibName: "XS_MineVC", bundle: Bundle.main))
        ])
        title = " "
        
        guard XS_Singleton.shared.infoModel != nil else { return }
        //更新用户信息
        XS_NetWork.requestJson("/user/getCurrUser")
            .mapModel(type: XS_UserBOM.self) // 返回 模型对象 和原始数据
            .subscribe(onNext: { model, resp in
                if let models = model, model != nil {
                    XS_Singleton.shared.infoModel?.userBO = models
                }
            }).disposed(by: disposeBag)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: animated)
    }
    
    private func addChildren(_ items: [tabBarM]) {
        for item in items {
            item.vc.shouldNavBarHidden = true
            item.vc.tabBarItem.image = UIImage(named: item.img)!.withRenderingMode(.alwaysOriginal)
            item.vc.tabBarItem.selectedImage = UIImage(named: item.select)!.withRenderingMode(.alwaysOriginal)
            item.vc.tabBarItem.title = item.title
//            item.vc.tabBarItem.imageInsets = UIEdgeInsets(top: 3, left: 0, bottom: 3, right: 0)
            addChild(item.vc)
        }
    }
}
